<template>
	<view v-if="is_load">
		<!-- 切换考试专业 -->
		<view class="cu-bar bg-white search ">
			<view class="action" @tap="goTo('city/city?first_visit=1')" style="font-size: 27rpx; max-width: 33.3333%">
				<text class="cuIcon-location text-blue" style="margin: 0"></text>
				<text class="text-blue text-cut">{{ currentCity.name }}</text>
			</view>
			<view class="action" @tap="goTo('exam/exam')" style="font-size: 27rpx; max-width: 33.3333%">
				<text class="cuIcon-group text-blue" style="margin-right: 6rpx"></text>
				<text class="text-blue text-cut">{{ currentExam.name }}</text>
			</view>
			<view class="action" @tap="goTo('profession/profession')" style="font-size: 27rpx; max-width: 33.3333%">
				<text class="cuIcon-read text-blue" style="margin-right: 6rpx"></text>
				<text class="text-blue text-cut">{{ currentProfession.name || '选择专业' }}</text>
			</view>
		</view>


		<!-- 封面图轮播 -->
		<view class="swiper-container bg-white margin-top-xs radius shadow-warp">
			<swiper class="swiper" indicator-dots autoplay circular :interval="3000" :duration="500">
				<swiper-item v-for="(item, index) in swiperList" :key="index" @tap="handleSwiperClick(item)">
					<image class="swiper-image" :src="item.img_url" mode="aspectFill"></image>
				</swiper-item>
			</swiper>
		</view>
		

		<!-- 考试倒计时提示 -->
		<view v-if="examTime.show" class="exam-countdown" style="margin-top: 14rpx;">
			<view class="countdown-content">
				<view class="countdown-header">
					<text class="cuIcon-time text-blue"></text>
					<text class="countdown-title">考试倒计时</text>
				</view>
				<view class="countdown-info">
					<view class="countdown-date">考试日期：{{ examTime.exam_date }}</view>
					<view class="countdown-timer">
						<text class="countdown-unit">还剩</text>
						<text class="countdown-days">{{ examTime.exam_remain }}</text>
						<text class="countdown-unit">天</text>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 新的宫格列表设计 -->
		<view>
			<view class="cu-bar bg-white margin-top-xs">
				<view class="action sub-title">
					<text class="text-xl text-bold text-blue text-shadow">学习中心</text>
					<text class="text-ABC text-blue">Study</text>
				</view>
						
				<view class="action share-btn-container">
				<button class="modern-share-btn" open-type="share">
					<text class="share-text">分享</text>
					<text class="cuIcon-share share-icon"></text>
				</button>
			</view>
			</view>
			<view class="modern-grid  bg-white  radius shadow-warp">
				<view @tap="goTo(item.page_uri)" class="modern-grid-item" v-for="(item, index) in menuList" :key="index">
					<image :src="item.icon" mode="aspectFit" class="modern-grid-icon"></image>
					<text class="modern-grid-text">{{ item.name }}</text>
				</view>
			</view>
		</view>
		
		<!-- 最新资讯组件 -->
		<index-article-list  v-if="!appIsAudit" />
		
	</view>
</template>

<script>
import { post, get } from '@/common/js/http.js';
import { navigateByLinkType } from '@/common/js/util.js';
import IndexArticleList from './index-article-list.vue';

export default {
	components: {
		IndexArticleList
	},
	name: 'Index2',
	props: {
		index_page_set: {
			type: Object,
			default: () => ({})
		}
	},
	data() {
		return {
			is_load: false,
			currentCity: {},
			currentExam: {},
			currentProfession: {},
			appIsAudit: true, // Default value, will be updated in created
			appPlatform: '', // Default value
			examTime: {
				show: false,
				exam_date: '',
				exam_remain: 0
			},
			menuList: [],
			swiperList: [] // 轮播图数据
		};
	},
	async created() {
        const app = getApp();
        this.appIsAudit = app.globalData.appIsAudit;
        this.appPlatform = app.globalData.appPlatform;

		console.log('Index2组件created钩子执行, appIsAudit值:', this.appIsAudit);
		this.initIndexExamData();
		// 等待两个异步方法都完成后再设置is_load为true
		await Promise.all([
			this.loadHomeData(),
			this.loadSwiperData()
		]);
		this.is_load = true;
		console.log('Index2组件数据加载完成, is_load:', this.is_load);
	},
	methods: {
		initIndexExamData() {
			const app = getApp();
			let cache = app.globalData.config.storage;
			this.currentCity = cache.getCurrentCityData();
			console.log('从缓存中读取城市数据:', this.currentCity);
			this.currentExam = cache.getCurrentExamData();
			this.currentProfession = cache.getCurrentProfessionData();
			
			if (!this.currentCity || !this.currentCity.id || Object.keys(this.currentCity).length === 0) {
				console.log('当前城市数据为空或无效，跳转到城市选择页面');
				uni.redirectTo({
					url: '/pages/index/city/city',
					fail: function(err) {
						console.error('跳转到城市选择页面失败:', err);
					}
				});
				return;
			}
		},

		async loadHomeData() {
			try {
				console.log('开始获取数据');
				const res = await get('home');
				console.log('获取数据成功11111：', res.data);
				if (res.data.examTime) {
					this.examTime = res.data.examTime;
				}
				if (res.data.menuList) {
					this.menuList = res.data.menuList;
				}
			} catch (e) {
				console.log('获取数据失败', e);
			}
		},

		goTo(url) {
			uni.navigateTo({ url });
		},

		async loadSwiperData() {
			try {
				const swiperRes = await get('swiper/get');
				console.log('获取轮播图成功：', swiperRes.data);
				this.swiperList = swiperRes.data || [];
				console.log('轮播图数据：', this.swiperList);
			} catch (error) {
				console.error('获取轮播图失败:', error);
				this.swiperList = [];
			}
		},

		handleSwiperClick(item) {
			if (item.target_url) {
				navigateByLinkType(item.target_type, item.target_url);
			}
		}
	}
};
</script>

<style src="./index2.css"></style>